<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>课时32_前端控制器模式</title>
</head>

<body>
    <div class="course-content">
        <div class="current-component">
            <div class="text markdown-body">
                <div>
                    <h1
                        style="border:0px;margin:0px 0px 10px;padding:0px;font-size:2.1em;font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        前端控制器模式</h1>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        前端控制器模式（Front Controller
                        Pattern）是用来提供一个集中的请求处理机制，所有的请求都将由一个单一的处理程序处理。该处理程序可以做认证/授权/记录日志，或者跟踪请求，然后把请求传给相应的处理程序。以下是这种设计模式的实体。
                    </p>

                    <ul class="list list-paddingleft-2" style="list-style-type:none;">
                        <li>
                            <p><strong>前端控制器（Front Controller）</strong>&nbsp;- 处理应用程序所有类型请求的单个处理程序，应用程序可以是基于 web
                                的应用程序，也可以是基于桌面的应用程序。</p>
                        </li>
                        <li>
                            <p><strong>调度器（Dispatcher）</strong>&nbsp;- 前端控制器可能使用一个调度器对象来调度请求到相应的具体处理程序。</p>
                        </li>
                        <li>
                            <p><strong>视图（View）</strong>&nbsp;- 视图是为请求而创建的对象。</p>
                        </li>
                    </ul>
                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        实现</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        我们将创建&nbsp;<em>FrontController</em>、<em>Dispatcher</em>&nbsp;分别当作前端控制器和调度器。<em>HomeView</em>&nbsp;和&nbsp;<em>StudentView</em>&nbsp;表示各种为前端控制器接收到的请求而创建的视图。
                    </p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>FrontControllerPatternDemo</em>，我们的演示类使用&nbsp;<em>FrontController</em>&nbsp;来演示前端控制器设计模式。
                    </p>

                    <p><img alt="" src="https://developer.aliyun.com/files/course/2017/09-24/132234ad0cbb110187.jpg">
                    </p>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 1</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建视图。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>HomeView.java</em>
                    </p>

                    <pre><code class="language-java">public class HomeView {
   public void show(){
      System.out.println("Displaying Home Page");
   }}</code></pre>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>StudentView.java</em>
                    </p>

                    <pre><code class="language-java">public class StudentView {
   public void show(){
      System.out.println("Displaying Student Page");
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 2</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建调度器 Dispatcher。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>Dispatcher.java</em>
                    </p>

                    <pre><code class="language-java">public class Dispatcher {
   private StudentView studentView;
   private HomeView homeView;
   public Dispatcher(){
      studentView = new StudentView();
      homeView = new HomeView();
   }

   public void dispatch(String request){
      if(request.equalsIgnoreCase("STUDENT")){
         studentView.show();
      }else{
         homeView.show();
      }	
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 3</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        创建前端控制器 FrontController。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>Context.java</em>
                    </p>

                    <pre><code class="language-java">public class FrontController {
	
   private Dispatcher dispatcher;

   public FrontController(){
      dispatcher = new Dispatcher();
   }

   private boolean isAuthenticUser(){
      System.out.println("User is authenticated successfully.");
      return true;
   }

   private void trackRequest(String request){
      System.out.println("Page requested: " + request);
   }

   public void dispatchRequest(String request){
      //记录每一个请求
      trackRequest(request);
      //对用户进行身份验证
      if(isAuthenticUser()){
         dispatcher.dispatch(request);
      }	
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 4</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        使用&nbsp;<em>FrontController</em>&nbsp;来演示前端控制器设计模式。</p>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        <em>FrontControllerPatternDemo.java</em>
                    </p>

                    <pre><code class="language-java">public class FrontControllerPatternDemo {
   public static void main(String[] args) {
      FrontController frontController = new FrontController();
      frontController.dispatchRequest("HOME");
      frontController.dispatchRequest("STUDENT");
   }}</code></pre>

                    <h2
                        style="border:0px;margin:2px 0px;padding:0px;font-size:1.8em;line-height:1.8em;color:rgb(51,51,51);font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, STHeiti, 'Microsoft Yahei', sans-serif;white-space:normal;background-color:rgb(255,255,255);">
                        步骤 5</h2>

                    <p
                        style="border:0px;margin-top:0px;margin-bottom:0px;padding:0px;line-height:2em;font-size:13px;font-family:'Microsoft Yahei', 'Helvetica Neue', Helvetica, Arial, sans-serif;color:rgb(51,51,51);white-space:normal;background-color:rgb(255,255,255);">
                        验证输出。</p>

                    <pre><code class="language-java">Page requested: HOME
User is authenticated successfully.
Displaying Home Page
Page requested: STUDENT
User is authenticated successfully.
Displaying Student Page</code></pre>

                </div>
            </div>
        </div>
    </div>
</body>

</html>